package com.onix.scalecamerapreview.translate;

import java.util.ArrayList;
import java.util.List;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class Translate {

    private Translate(){}

    private static final class SingleHold
    {
      private static final Translate sInstance = new Translate();
    }
    private static final List<Textbean> textbeans = new ArrayList<>();
    private static  Textbean textbean = new Textbean();
    static {
        textbeans.add(textbean);
    }

    DisposableObserver<List<TranslateResult>> disposableObserver=null;
    List<SubscriberOnNextListener<List<TranslateResult>>> sipmleOnNextListeners = new ArrayList<>();

    public static Translate getInstance(){
        return SingleHold.sInstance;
    }


    public  void execute(String text, String toLanguage)  {
        textbean.Text = text;
        disposableObserver =new DisposableObserver<List<TranslateResult>>() {
            @Override
            protected void onStart() {
                super.onStart();
                for (SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener : sipmleOnNextListeners) {
                    sipmleOnNextListener.onStrat();
                }
            }

            @Override
            public void onNext(List<TranslateResult> translateResults) {
                for (SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener : sipmleOnNextListeners) {
                    sipmleOnNextListener.onScuss(translateResults);
                }
            }

            @Override
            public void onError(Throwable e) {
                for (SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener : sipmleOnNextListeners) {
                    sipmleOnNextListener.onError(e);
                }
                onComplete();
            }

            @Override
            public void onComplete() {
                for (SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener : sipmleOnNextListeners) {
                    sipmleOnNextListener.onCompleted();
                }
            }
        };
        RetrofitUtil.getInstance().get(Request.class)
                .translate(toLanguage, textbeans)
                .subscribeOn(Schedulers.io())
                .subscribe(disposableObserver);
    }

    public void unBinde(SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener){
        sipmleOnNextListeners.remove(sipmleOnNextListener);
        cancel();
    }

    public void bindListener(SubscriberOnNextListener<List<TranslateResult>> sipmleOnNextListener){
        sipmleOnNextListeners.add(sipmleOnNextListener);
    }
    //取消
    public void cancel(){
        if(disposableObserver!=null&&!disposableObserver.isDisposed()){
            disposableObserver.dispose();
            disposableObserver.onComplete();
            disposableObserver =null;
        }
    }

}
